package frame.service.context;

import frame.service.standard.Constants;
import http.RedisSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServiceContext {
    /**
     * 当前交易的请求相关数据
     *
     * 请求源数据字节数组
     * reqData.put(Constants.REQUEST_BODY_BYTE, request);
     *
     * 源请求http头
     * reqData.put(Constants.REQUEST_HEADERS, headers);
     *
     * 源请求QueryString
     * reqData.put(Constants.REQUEST_QUERY_STRING, servletRequest.getQueryString());
     *
     * 源请求方法
     * reqData.put(Constants.REQUEST_HTTP_METHOD, servletRequest.getMethod().toLowerCase());
     *
     * servletRequest attributes
     * reqData.put(Constants.REQUEST_ATTR, requestAttr);
     *
     * 请求源数据
     * reqData.put(Constants.REQUEST_MSG_CONTENT, res);
     *
     * 请求URL相关内容
     * reqData.put(Constants.REQUEST_SAFE_MODE, safeMode);
     * reqData.put(Constants.REQUEST_MODULE, module);
     * reqData.put(Constants.REQUEST_SERVICE, service);
     * reqData.put(Constants.REQUEST_ACTION, action);
     * reqData.put(Constants.REQUEST_VERSION, version);
     *
     * @see Constants
     * */
    private Map<String, Object> reqData = new HashMap<>(1);
    /**
     * 当前交易响应数据
     * */
    private Map<String, Object> respData = new HashMap<>(1);

    //交易处理状态码
    private String statusCode = "000000";
    //交易处理结果描述
    private String respMsg = "success";
    //当前交易解析完成的contentType
    private String contentType;
    //当前交易的QueryString
    private Map<String,String> queryParamMap;
    //当前交易所属会话
    private RedisSession redisSession;

    public String getReqContentString(String key){
        Object o = getReqContent(key);
        if(o!=null && o instanceof String){
            return (String) o;
        }else{
            return o==null?null:o.toString();
        }
    }
    public List getReqContentList(String key){
        Object o = getReqContent(key);
        if(o!=null && o instanceof List){
            return (List)o;
        }
        return null;
    }
    public Map getReqContentMap(String key){
        Object o = getReqContent(key);
        if(o!=null && o instanceof Map){
            return (Map)o;
        }
        return null;
    }

    //获取请求参数值
    public Object getReqContent(String key){
        Object ret = null;
        Object reqKeyData = reqData.get(key);
        if(reqKeyData != null){
            return reqKeyData;
        }

        Object o = reqData.get(Constants.REQUEST_MSG_CONTENT);
        if(o!=null && o instanceof Map){
            Map<String, Object> dataS = (Map)o;
            ret = dataS.get(key);
        }
        if(ret!=null){
            return ret;
        }else{
            String queryString = (String) reqData.get(Constants.REQUEST_QUERY_STRING);
            if(queryParamMap == null){
                queryParamMap = parseQueryString(queryString);
            }
            if(queryParamMap!=null){
                ret = queryParamMap.get(key);
            }
        }
        return ret;
    }

    //解析queryString
    private Map<String, String> parseQueryString(String queryString){
        if(queryString!=null){
            String[] vals = queryString.split("&");
            if(vals!=null && vals.length>0){
                Map<String,String> ret = new HashMap<>();
                for (String s:vals){
                    String[] kv = s.split("=");
                    if(kv.length==2){
                        ret.put(kv[0], kv[1]);
                    }
                }
                return ret;
            }
        }
        return null;
    }

    public RedisSession getRedisSession() {
        return redisSession;
    }

    public void setRedisSession(RedisSession redisSession) {
        this.redisSession = redisSession;
    }

    public void setReqData(Map<String, Object> reqData) {
        this.reqData.putAll(reqData);
    }

    public Map<String, Object> getReqData() {
        return reqData;
    }

    public void setRespDataVal(String key, String val){
        respData.put(key, val);
    }

    public void setRespData(Map<String, Object> respData) {
        this.respData.putAll(respData);
    }
    public Map<String, Object> getRespData() {
        return respData;
    }

    public String getStatusCode() {
        return statusCode;
    }
    public String getRespMsg() {
        return respMsg;
    }
    public String getContentType() {
        return contentType;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }
    public void setRespMsg(String respMsg) {
        this.respMsg = respMsg;
    }
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
}
